package com.action.gulimall.seckill.web;

import com.action.common.utils.R;
import com.action.gulimall.seckill.service.SeckillService;
import com.action.gulimall.seckill.to.SeckillSkuRedisTo;
import com.action.gulimall.seckill.vo.SeckillSkuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author sl shilun217@qq.com
 * @Deacription TODO
 * @date 2021/07/04/20:26
 **/
@Controller
public class WebSeckillController {
    @Autowired
    SeckillService seckillService;
    @ResponseBody
    @GetMapping("/hello")
    public R hello(){
        return R.ok();
    }

    @ResponseBody
    @GetMapping("/getCurrentSeckillSkus")
    public R getCurrentSeckillSkus(){
        //获取到当前可以参加秒杀商品的信息
        List<SeckillSkuRedisTo> seckillSkuRedisTos = seckillService.getCurrentSeckillSkus();
        return R.ok().setData(seckillSkuRedisTos);
    }

    @ResponseBody
    @GetMapping("/seckillsku/info/{skuId}")
    public R seckillSkuInfoBySkuId(@PathVariable("skuId") Long skuId){
        SeckillSkuVo seckillSkuVo = seckillService.getSeckillInfoBySkuId(skuId);
        return R.ok().setData(seckillSkuVo);
    }
    //"http://seckill.gulimall.com/kill?killId=" + killId + "&key=" + code + "&num=" + num

    @GetMapping("/kill")
    public String kill(@RequestParam("killId") String killId,@RequestParam("key") String key,@RequestParam("num") Integer num,Model model){
        String orderSn = seckillService.kill(killId,key,num);
        model.addAttribute("orderSn", orderSn);
        return "success";
    }
}
